package com.example.recyclerview.mian;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.widget.Toast;

import com.example.recyclerview.R;
import com.example.recyclerview.adapter.MainAdapter;
import com.example.recyclerview.adapter.MainProviderMultiAdapter;
import com.example.recyclerview.bean.MainBean;
import com.example.recyclerview.bean.MultiMainBean;
import com.example.recyclerview.recycler.BaseRecyclerAdapter;
import com.example.recyclerview.recycler.DividerItemDecoration;
import com.example.recyclerview.recycler.slide.BaseItemTouchHelpCallback;
import com.example.recyclerview.recycler.slide.BaseSlideAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    List<MainBean> newList = new ArrayList<>();

    private MainAdapter mainAdapter;
    private List<MainBean> mainBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainBeanList = new ArrayList<>();
        mainBeanList.add(new MainBean(0, "100"));
        mainBeanList.add(new MainBean(1, "200"));
        mainBeanList.add(new MainBean(2, "300"));
        mainBeanList.add(new MainBean(2, "300"));
        mainBeanList.add(new MainBean(2, "300"));
        mainBeanList.add(new MainBean(2, "300"));
        mainBeanList.add(new MainBean(2, "300"));
        mainBeanList.add(new MainBean(2, "300"));
        mainBeanList.add(new MainBean(2, "300"));
        mainBeanList.add(new MainBean(2, "300"));
        mainBeanList.add(new MainBean(2, "300"));

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        mainAdapter = new MainAdapter(R.layout.item_main, mainBeanList);
        //非必要，加载数据时是否显示动画，false为总是显示，默认为true
        mainAdapter.isFirstOnly(false);
        //非必要，设置动画时间，默认1000毫米
        mainAdapter.setDuration(1000);
        //非必要，设置插值器动画更丝滑
        mainAdapter.setInterpolator(new AccelerateDecelerateInterpolator());
        //开启默认动画
        mainAdapter.openLoadAnimation(BaseRecyclerAdapter.SLIDE_IN_RIGHT);

        recyclerView.addItemDecoration(new DividerItemDecoration(10));
//        recyclerView.addItemDecoration(new DividerItemDecoration(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent), 10));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mainAdapter);

        //item点击
        mainAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                mainAdapter.notifyDataSetChanged();
            }
        });
        //item长按
        mainAdapter.setOnItemLongClickListener(new BaseRecyclerAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View view, int position) {
                Toast.makeText(getApplicationContext(), "长按" + position, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        // 先注册需要点击的子控件id（注意，请不要写在convert方法里）
        mainAdapter.addChildClickViewIds(R.id.tv_name);
        // 设置子控件点击监听
        mainAdapter.setOnItemChildClickListener(new BaseRecyclerAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseRecyclerAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.tv_name) {
                    List<MainBean> newBeanList = new ArrayList<>();
                    newBeanList.add(new MainBean(0, "0-800"));
                    newBeanList.add(new MainBean(5, "5-500"));
                    newBeanList.add(new MainBean(1, "1-200"));
                    newBeanList.add(new MainBean(3, "3-300"));
                    newBeanList.add(new MainBean(3, "3-300"));
                    mainAdapter.setDiffData(newBeanList);
                }
            }
        });
        // 先注册需要长按的子控件id（注意，请不要写在convert方法里）
        mainAdapter.addChildLongClickViewIds(R.id.tv_name);
        // 设置子控件长按监听
        mainAdapter.setOnItemChildLongClickListener(new BaseRecyclerAdapter.OnItemChildLongClickListener() {
            @Override
            public boolean onItemChildLongClick(@NonNull BaseRecyclerAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.tv_name) {
                    Toast.makeText(getApplicationContext(), "子view长按" + position, Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });

//        mainAdapter.setOnLoadMoreListener(recyclerView, new BaseRecyclerAdapter.OnLoadMoreListener() {
//            @Override
//            public void onLoadMoreListener() {
//                //加载中
//                mainAdapter.setLoadingView(R.layout.loading_view);
////                。。。
//                //模拟加载失败
//                mainAdapter.setLoadFailedView(R.layout.load_failed_view);
//            }
//
//            //点击加载失败触发
//            @Override
//            public void onLoadFailedListener() {
//                //模拟加载成功
//                MainBean mainBean = new MainBean(4, "100");
//                mainAdapter.addNewItem(mainBean);
//                mainAdapter.setLoadSuccessView();
//                //模拟没有更多
////              mainAdapter.setLoadEndView(R.layout.load_end_view);
//            }
//        });

        ProviderMulti();
    }

    private void ProviderMulti() {
        List<MultiMainBean> multiMainList = new ArrayList<>();
        multiMainList.add(new MultiMainBean(R.layout.item_main, 0, "父0"));
        multiMainList.add(new MultiMainBean(R.layout.item_main2, 1, "子1"));
        multiMainList.add(new MultiMainBean(R.layout.item_main2, 2, "子2"));
        multiMainList.add(new MultiMainBean(R.layout.item_main, 3, "父1"));
        multiMainList.add(new MultiMainBean(R.layout.item_main2, 4, "子1"));
        multiMainList.add(new MultiMainBean(R.layout.item_main2, 5, "子2"));

        RecyclerView multiRecyclerView = findViewById(R.id.multiRecyclerView);
        MainProviderMultiAdapter mainProviderMultiAdapter = new MainProviderMultiAdapter(multiMainList);
        multiRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        multiRecyclerView.setAdapter(mainProviderMultiAdapter);
    }
}